﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IntroduceCompany.Singleton;
using IntroduceCompany.Util;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;

namespace IntroduceCompany.Models
{
    public class VideoView_Validation
    {
        //[System.ComponentModel.DataAnnotations.MetadataType(typeof(IntroduceCompany.Models.VideoView_Validation))]  put in Article class of linq

        [Required(ErrorMessage = "Tiêu đề Không được để trống")]
        public string VideoTitle { get; set; }

        [Required(ErrorMessage = "Video link Không được để trống")]
        public string VideoURL { get; set; }
    }

    public class VideoModels
    {
  #region BackEnd_Video

        internal IQueryable<VideoView> GetVideo(string languageID)
        {
            if (languageID == ConstantClass.markViewAllLanguage)
            {
                return DataSingleton.GetInstance().VideoViews.OrderByDescending(u => u.VideoID);
            }
            else
            {
                return DataSingleton.GetInstance().VideoViews.Where(u => u.LanguageID == languageID).OrderByDescending(u => u.VideoID);
            }
        }

        /// <summary>
        /// Change Video visible
        /// </summary>
        /// <param name="videoID"></param>
        /// <param name="videoVisible"></param>
        internal void ChangeVideoVisible(int videoID, bool videoVisible)
        {
            Video video = DataSingleton.GetInstance().Videos.SingleOrDefault(u => u.VideoID == videoID);
            video.VideoVisible = (!videoVisible);
            DataSingleton.GetInstance().Videos.Context.SubmitChanges();
        }

        internal VideoView GetVideoByVideoIDAndLanguageID(int videoID, String languageID)
        {
            return DataSingleton.GetInstance().VideoViews.SingleOrDefault(u => u.VideoID == videoID && u.LanguageID == languageID);
        }

        internal Video GetVideoByID(int videoID)
        {
            return DataSingleton.GetInstance().Videos.SingleOrDefault(u => u.VideoID == videoID);
        }

        internal void VideoEdit(VideoView _EditedVideoView)
        {
            Video _OldVideo = DataSingleton.GetInstance().Videos.SingleOrDefault(u => u.VideoID == _EditedVideoView.VideoID);
         // _OldVideo.VideoURL = _EditedVideoView.VideoURL;
            _OldVideo.UserName = _EditedVideoView.Username;
            _OldVideo.VideoDate = DateTime.Now;
            DataSingleton.GetInstance().Videos.Context.SubmitChanges();

            Video_L _OldVideo_L = DataSingleton.GetInstance().Video_Ls.SingleOrDefault(u => u.VideoID == _EditedVideoView.VideoID && u.LanguageID == _EditedVideoView.LanguageID);
            _OldVideo_L.VideoTitle = _EditedVideoView.VideoTitle;
            _OldVideo_L.VideoDescription = _EditedVideoView.VideoDescription;
            DataSingleton.GetInstance().Video_Ls.Context.SubmitChanges();
           
            DataSingleton.GetInstance().Refresh(RefreshMode.KeepChanges, _OldVideo_L);
        }

        /// <summary>
        /// update yuotube video code to sql DB
        /// </summary>
        /// <param name="_EditedVideoView"></param>
        internal void UpdateVideoLinkCode(VideoView _EditedVideoView)
        {
            Video _OldVideo = DataSingleton.GetInstance().Videos.SingleOrDefault(u => u.VideoID == _EditedVideoView.VideoID);
            _OldVideo.VideoURL = _EditedVideoView.VideoURL;
            _OldVideo.UserName = _EditedVideoView.Username;
            _OldVideo.VideoDate = DateTime.Now;
            DataSingleton.GetInstance().Videos.Context.SubmitChanges();
        }

        internal int VideoCreate(VideoView _NewOneVideo)
        {
            return DataSingleton.GetInstance().sp_VideoInsert(_NewOneVideo.VideoURL, _NewOneVideo.Username);
        }

        internal void Video_L_Create(int videoID, String languageID, String title, String des)
        {
            try
            {
                DataSingleton.GetInstance().sp_Video_L_Insert(videoID, languageID, title, des);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// cut string video link (youtube)http://www.youtube.com/watch?feature=endscreen&v=ceVlltPBcHg&NR=1
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        internal String CutVideoURL(String str)
        {
            if (str.IndexOf("youtube") == -1)
            {
                str = "";
            }

            if (str.Length > 8)
            {
                if (str.IndexOf("&") == -1)
                {
                    return str.Substring(str.IndexOf("watch?v=") + 8);
                }
                else
                {
                    if (str.IndexOf("&v=") != -1 && str.IndexOf("feature") != -1)
                    {
                        return str.Substring(str.IndexOf("&v=")+3, 11);
                    }
                    else
                    {
                        return str.Substring(str.IndexOf("watch?v=") + 8, str.IndexOf("&") - (str.IndexOf("watch?v=") + 8));
                    }
                }
            }
            else {
                return str;
            }
        }


        internal void VideoDelete(int videoID)
        {
            Video video = DataSingleton.GetInstance().Videos.SingleOrDefault(u => u.VideoID == videoID);
            DataSingleton.GetInstance().Videos.DeleteOnSubmit(video);
            DataSingleton.GetInstance().SubmitChanges();
        }

  #endregion

///////////////////////////////////////////////////////////////////////////////////////

  #region FrontEnd_Video

        internal VideoView Frontend_GetVideoByVideoIDAndLanguageID(int videoID, String languageID)
        {
            return DataSingleton.GetInstance().VideoViews.SingleOrDefault(u => u.VideoID == videoID && u.LanguageID == languageID && u.VideoVisible ==true);
        }

        internal IQueryable<VideoView> Frontend_GetVideo(string languageID)
        {
            return DataSingleton.GetInstance().VideoViews.Where(u => u.LanguageID == languageID && u.VideoVisible==true).OrderByDescending(u => u.VideoID);
        }

        internal IQueryable<VideoView> Frontend_GetOnePageVideo(string languageID, string pageIndex, int step)
        {
            int startRowIndex = (Convert.ToInt16(pageIndex) - 1) * step;
            return DataSingleton.GetInstance().VideoViews.Where(u => u.LanguageID == languageID && u.VideoVisible == true).OrderByDescending(u => u.VideoID).Skip(startRowIndex).Take(step);
        }

        internal int Frontend_GetTotalRowsOneLanguage(string languageID)
        {
            return DataSingleton.GetInstance().VideoViews.Where(u => u.LanguageID == languageID && u.VideoVisible == true).Count();
        }

        /// <summary>
        /// get a video object form table video 
        /// </summary>
        /// <param name="videoID"></param>
        /// <returns></returns>
        internal Video Frontend_GetVideoWithoutLanguage(int videoID)
        {
            return DataSingleton.GetInstance().Videos.SingleOrDefault(u=>u.VideoID == videoID && u.VideoVisible == true);
        }

        /// <summary>
        /// get video id by video title SEO URL
        /// </summary>
        /// <param name="video_SEO_URL"></param>
        /// <returns></returns>
        internal int GetVideoID_By_Video_SEO_URL(string video_SEO_URL)
        {
            return DataSingleton.GetInstance().Video_Ls.SingleOrDefault(u => u.VideoSeoUrl == video_SEO_URL).VideoID;
        }

        /// <summary>
        /// get video title SEO URL by videoID and languageID
        /// </summary>
        /// <param name="videoID"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        internal string GetVideo_SEO_URL_By_VideoID_LanguageID(int videoID, string languageID)
        {
            return DataSingleton.GetInstance().Video_Ls.SingleOrDefault(u=>u.VideoID == videoID && u.LanguageID == languageID).VideoSeoUrl;
        }

        /// <summary>
        /// check if a video has that language
        /// </summary>
        /// <param name="videoID"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        internal bool CheckVideo_WithLanguage(int videoID, string languageID)
        {
            return DataSingleton.GetInstance().Video_Ls.Any(u => u.VideoID == videoID && u.LanguageID == languageID);
        }
  #endregion



        
    }
}